$(function () {
    /*flash2 组件*/
    $.fn.flash2 = function (obj) {
        let _self = this
        let def = {
            data: [],
            index: 0
        }
        _self.obj = $.extend(def, obj)
        data = _self.obj.data
        let html = ''
        data.map((value, index) => {
            html += '<div style="display:' + (index === 0 ? 'block' : 'none') + '"><a href="' + value.link + '" target="_blank"><img src="' + value.imgsrc + '" alt=""/></a></div>'
        })
        _self.outer = $(this)
        _self.outer.html(html)

        let autoMove = () => {
            if (_self.obj.index === _self.outer.find('div').length-1) {
                _self.obj.index = 0
            } else {
                _self.obj.index ++
            }
            _self.outer.find('div:eq('+_self.obj.index+')').show().siblings().hide()
            _self.auto = setTimeout(autoMove, 2000)
        }
        _self.outer.hover(function () {
            clearTimeout(_self.auto)
        }, function () {
            _self.auto = setTimeout(autoMove, 2000);
        }).trigger('mouseleave')
        return this
    }
    /*向上滚动 组件*/
    $.fn.scroll = function (obj) {
        let _self = this
        let def = {
        }
        _self.obj = $.extend(def, obj)

        let scrollObj = $(this)
        let scrollOut = scrollObj.parent()
        // 高度小于外层高度，不滚动
        if (scrollObj.height() <= scrollOut.height()) {
            return
        }
        // 滚动事件
        let autoMove = () => {
            scrollObj.stop().animate({ 'top': '-30px' }, 1000, function () {
                $(this).append($(this).find('li:first')).css('top', 0)
                _self.auto = setTimeout(autoMove, 1000)

            })
        }
        // 鼠标移上外层停止滚动，鼠标离开继续滚动
        scrollOut.hover(function () {
            clearTimeout(_self.auto)
        }, function () {
            _self.auto = setTimeout(autoMove, 1000)
        }).trigger('mouseleave')
        return this
    }
    let index = {
        url: {
            getNowDate: 'front/nowDate',
            getList: 'front/index',
            topFlash: 'front/topFlash',
            middleFlash: 'front/middleFlash',
            bottomFlash: 'front/bottomFlash',
            getLinks: 'front/friendlyLink'
        },
        init: function () {
            // 显示当前日期
            this.nowDate()
            this.toLists()
            this.toLinks()
            
            // 资质平台
            $('.query_searchbtn').click(function() {
                let type = $(this).attr('type')
                let searchkey = $.trim($(this).parent().find('input').val())
                console.log(searchkey)
                if (searchkey === '') {
                    alert('请输入查询内容')
                    return
                }
                window.location.href = 'query.jsp?type=' + type + '&key=' + encodeURI(encodeURI(searchkey))
            })
            // 顶部flash
            $.post(index.url.topFlash, {}, (data) => {
                if (data.success) {
                    this.toFlash1.init({
                        data: data.result,
                        outer: '#flash1'
                    })
                }
            })
            // 中部flash
            $.post(index.url.middleFlash, {}, (data) => {
                if (data.success) {
                    $('#flash2').flash2({
                        data: data.result
                    })
                }
            })
            // 底部flash
            $.post(index.url.bottomFlash, {}, (data) => {
                if (data.success) {
                    $('#flash3').flash2({
                        data: data.result
                    })
                }
            })
            // 搜索
            $('#searchBtn').click(() => {
                let searchkey = $.trim($('#searchInput').val())
                if (searchkey == '') {
                    alert('请输入搜索关键字')
                    return
                }
                window.location.href = 'list.jsp?inputVal=' + encodeURI(encodeURI(searchkey))
            })
        },
        nowDate: () => {
            $.post(index.url.getNowDate, {}, (data) => {
                if (data.success) {
                    $('#nowDate').html(data.result)
                }
            })
        },
        toLists: () => {
            $.post(index.url.getList, {}, (data) => {
                if (data.success) {
                    let res = data.result
                    for (let i in res) {
                        let list = res[i]
                        let arr = []
                        switch (i) {
                            case 'news':
                                $('.news_first').html(
                                    '<h3>' + list[0].title + '</h3>' +
                                    '<p>' + list[0].intro + '<a href="' + list[0].link + '" class="red">[查看详情]</a></p>'
                                )
                                arr = []
                                list.map((value, index) => {
                                    if (index > 0) {
                                        arr.push('<li><a href="' + value.link + '">' + value.title + '</a> <!--<span>[' + value.createdate + ']</span> --> </li>')
                                    }
                                })
                                $('.news').html(arr.join(''))
                                break;
                            case 'laws':
                                $('.laws_first').html(
                                    '<h3><a href="' + list[0].link + '">' + list[0].title + '</a></h3>' +
                                    '<p>' + list[0].intro + '</p>'
                                )
                                arr = []
                                list.map((value, index) => {
                                    if (index > 0) {
                                        arr.push('<li><a href="' + value.link + '">' + value.title + '</a></li>')
                                    }
                                })
                                $('.laws').html(arr.join(''))
                                break;
                            case 'info':
                                $('.info_first').html(
                                    '<div><a href="' + list[0].link + '"><img src="' + list[0].imgsrc + '" width="200px" height="110px"/><p>' + list[0].title + '</p></a></div>' +
                                    '<div><a href="' + list[1].link + '"><img src="' + list[1].imgsrc + '" width="200px" height="110px"/><p>' + list[1].title + '</p></a></div>'

                                )
                                arr = []
                                list.map((value, index) => {
                                    if (index > 1) {
                                        arr.push('<li><a href="' + value.link + '">' + value.title + '</a></li>')
                                    }
                                })
                                $('.info').html(arr.join(''))
                                break;
                            /* case 'association':
                                 $('.association_first').html(
                                     '<h3>' + list[0].title + '</h3>' +
                                     '<p>' + list[0].intro + '<a href="' + list[0].link + '" class="red">[查看详情]</a></p>'
                                 )
                                 break;
                                 */
                            default:
                                arr = []
                                list.map((value, index) => {
                                    arr.push('<li><a href="' + value.link + '">' + value.title + '</a></li>')
                                })
                                $('.' + i).html(arr.join(''))
                            }
                    }
                    $('#noticeScroll').scroll()
                }
            })
        },
        toFlash1: {
            init: (obj) => {
                let _self = index.toFlash1
                let def = {
                    data: [],
                    outer: 'body',
                    index: 0
                }
                _self.obj = $.extend(def, obj)
                data = _self.obj.data
                let html = '<div class="flash_top"><div class="flash_imgs">'
                data.map((value, index) => {
                    html += '<div index="' + index + '"><a href="' + value.link + '"><img src="' + value.imgsrc + '" alt=""/></a></div>'
                })
                html += '    	</div><div class="flash_btn">'
                data.map((value, index) => {
                    html += '<span class="' + (index === 0 ? 'now' : '') + '">' + (index + 1) + '</span>'
                })
                html += '	    </div></div><div class="flash_info">'
                data.map((value, index) => {
                    html += '<p style="display:' + (index === 0 ? 'block' : 'none') + '">' + value.title + '</p>'
                })
                html += '</div>'
                _self.outer = $(_self.obj.outer)
                _self.outer.html(html)
                _self.imgs = _self.outer.find('.flash_imgs div')
                _self.nums = _self.outer.find('.flash_btn span')
                _self.infos = _self.outer.find('.flash_info p')
                _self.nums.click(function() {
                    let curIndex = _self.outer.find('.flash_btn span').index($(this));
                    _self.moveFlash(curIndex, true)
                })
                _self.outer.hover(function () {
                    clearTimeout(_self.auto)
                }, function () {
                    _self.auto = setTimeout(_self.autoMove, 2000);
                }).trigger('mouseleave')
            },
            autoMove: () => {
                let _self = index.toFlash1
                let curIndex = (_self.obj.index >= _self.nums.length - 1) ? 0 : (_self.obj.index + 1)
                _self.moveFlash(curIndex, true)
                _self.auto = setTimeout(_self.autoMove, 2000)
            },
            moveFlash: (curIndex, dir) => {
                let _self = index.toFlash1
                _self.nums.eq(curIndex).addClass('now').siblings().removeClass('now')
                _self.infos.eq(curIndex).show().siblings().hide()
                if (dir) {
                    let newImg = _self.outer.find('.flash_imgs div[index = ' + curIndex + ']')
                    let oldImg = _self.outer.find('.flash_imgs div[index = ' + _self.obj.index + ']')
                    newImg.insertAfter(oldImg);
                    _self.outer.find('.flash_imgs').stop().animate({ 'left': '-315px' }, 500, function () {
                        $(this).append($(this).find(oldImg)).css('left', 0)
                    })
                }
                _self.obj.index = curIndex
            }
        },
        toLinks: () => {

            $.post(index.url.getLinks, {}, (data) => {
                if (data.success) {
                    let res = data.result
                    for (let i in res) {
                        let opt = []
                        res[i].map((value, index) => {
                            opt.push('<option value="' + value.link + '">' + value.title + '</option>')
                        })
                        $('.link' + i).html(opt.join(''))
                    }
                }
            })
        }
    }
    index.init()
})
function linkto(obj) {
    window.open($(obj).val())
}